<template>
	<div>
	<el-input :value="selVal_cn"  placeholder="请选择人员"   maxlength="20" >
		<el-button type="primary" @click="selUserDialogVisible=true" slot="append" icon="el-icon-plus" size="mini" />
	</el-input>
  <el-dialog title="选择用户" append-to-body :visible.sync="selUserDialogVisible" width="920px" >
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="selUserDeptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="selUserDeptOptions"
            :props="selUserDefaultProps"
            :expand-on-click-node="false"
            :filter-node-method="selUserFilterNode"
            ref="tree"
            default-expand-all
            @node-click="selUserHandleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="selUserQueryParams" ref="selUserQueryForm" :inline="true" v-show="selUserShowSearch" label-width="68px">
          <el-form-item label="用户名称" prop="userName">
            <el-input
              v-model="selUserQueryParams.userName"
              placeholder="请输入用户名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="selUserHandleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input
              v-model="selUserQueryParams.phonenumber"
              placeholder="请输入手机号码"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="selUserHandleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="selUserDateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="selUserHandleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="selUserResetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="selUserLoading" :data="selUserList"
		 @selection-change="selUserHandleSelectionChange"
		 @current-change="handleCurrentChange"
		 :highlight-current-row="selType==='index'"
		 >
          <el-table-column :type="selType" width="50" align="center" />
          <el-table-column label="用户编号" align="center" key="userId" prop="userId" v-if="selUserColumns[0].visible" />
          <el-table-column label="用户名称" align="center" key="userName" prop="userName" v-if="selUserColumns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" v-if="selUserColumns[2].visible" :show-overflow-tooltip="true" />
          <el-table-column label="部门" align="center" key="deptName" prop="dept.deptName" v-if="selUserColumns[3].visible" :show-overflow-tooltip="true" />
          <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" v-if="selUserColumns[4].visible" width="120" />
          <el-table-column label="状态" align="center" key="status" v-if="selUserColumns[5].visible">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="selUserHandleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="selUserColumns[6].visible" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="selUserTotal>0"
          :total="selUserTotal"
          :page.sync="selUserQueryParams.pageNum"
          :limit.sync="selUserQueryParams.pageSize"
          @pagination="getSelUserList"
        />
      </el-col>
    </el-row>
	<span slot="footer" class="dialog-footer">
		<el-button @click="selUserDialogVisible = false">取 消</el-button>
		<el-button type="primary" @click="selUserCloseDialog">确 定</el-button>
		</span>
	</el-dialog>  
 </div> 
</template>

<script>
import { listUser, getUser } from "@/api/system/user";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "SelUserPanel",
  components: { Treeselect },
  props:{
		selType:{
			type: String,
			default:'index'
		},
		value:String,
		val_cn:String,
		selUserDept:{
			type:Boolean,
			default:false
		},
  },
  data() {
    return {
		//按钮key
	  selUserKey:null,
		//显示dialog
	  selUserDialogVisible: false,
      // 遮罩层
      selUserLoading: true,
      // 选中数组
      selUserIds: [],
	  // 选中人员
	  selUser:null,
      // 显示搜索条件
      selUserShowSearch: true,
      // 总条数
      selUserTotal: 0,
      // 用户表格数据
      selUserList: null,
      // 部门树选项
      selUserDeptOptions: undefined,
      // 部门名称
      selUserDeptName: undefined,
      // 日期范围
      selUserDateRange: [],
      // 状态数据字典
      selUserStatusOptions: [],
      // 性别状态字典
      selUserSexOptions: [],
      // 岗位选项
      selUserPostOptions: [],
      selUserDefaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      selUserQueryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: 0,
        deptId: undefined
      },
      // 列信息
      selUserColumns: [
        { key: 0, label: `用户编号`, visible: true },
        { key: 1, label: `用户名称`, visible: true },
        { key: 2, label: `用户昵称`, visible: true },
        { key: 3, label: `部门`, visible: true },
        { key: 4, label: `手机号码`, visible: true },
        { key: 5, label: `状态`, visible: false },
        { key: 6, label: `创建时间`, visible: true }
      ],
    };
  },
  computed:{
	  selVal_cn(){
		  return this.val_cn?this.val_cn:this.value
	}
	
  },
  watch: {
    // 根据名称筛选部门树
    selUserDeptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getSelUserList();
    this.getSelUserTreeselect();
    this.getDicts("sys_normal_disable").then(response => {
      this.selUserStatusOptions = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.selUserSexOptions = response.data;
    });
    this.getConfigKey("sys.user.initPassword").then(response => {
      this._initPassword = response.msg;
    });
  },
  methods: {
    /** 查询用户列表 */
    getSelUserList() {
      this.selUserLoading = true;
      listUser(this.addDateRange(this.selUserQueryParams, this.selUserDateRange)).then(response => {
          this.selUserList = response.rows;
          this.selUserTotal = response.total;
          this.selUserLoading = false;
        }
      );
    },
    /** 查询部门下拉树结构 */
    getSelUserTreeselect() {
      treeselect().then(response => {
        this.selUserDeptOptions = response.data;
      });
    },
    // 筛选节点
    selUserFilterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    selUserHandleNodeClick(data) {
      this.selUserQueryParams.deptId = data.id;
      this.getSelUserList();
    },
    /** 搜索按钮操作 */
    selUserHandleQuery() {
      this.selUserQueryParams.page = 1;
      this.getSelUserList();
    },
    /** 重置按钮操作 */
    selUserResetQuery() {
      this.selUserDateRange = [];
      this.resetForm("selUserQueryForm");
      this.selUserHandleQuery();
    },
    // 多选框选中数据
    selUserHandleSelectionChange(selection) {
      this.selUsers = selection;
    },
	handleCurrentChange(val){
		this.selUser = val
	},
	selUserCloseDialog(){
		let userInfo = this.selType==="index"?this.selUser:this.selUsers;
		let val='';
		let val_cn='';
		if(Array.isArray(userInfo)){
			val = userInfo.map(item=>item.userName).join(',');
			val_cn = userInfo.map(item=>item.nickName).join(',');
		}else{
			val = userInfo.userName;
			val_cn = userInfo.nickName;
		}
		this.$emit('input', val)
		this.$emit('tousername', val_cn)
		if(this.selUserDept) {this.$emit('touserdept', userInfo.deptId)}
		this.selUserDialogVisible = false
	},
	/**选择用户**/
	selectUser(){
		this.selDialog=true;
	}
  }
};
</script>